package org.csu.onlineedubackend.controller;

import org.csu.onlineedubackend.dto.CourseDetailDTO;
import org.csu.onlineedubackend.entity.Chapter;
import org.csu.onlineedubackend.entity.Lesson;
import org.csu.onlineedubackend.mapper.ChapterMapper;
import org.csu.onlineedubackend.mapper.LessonMapper;
import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.result.ResponseCode;
import org.csu.onlineedubackend.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 学生课程控制器
 * 处理课程详情、进度等信息
 */
@RestController
@RequestMapping("/api/student/courses")
@Slf4j
public class CourseStudentController {

    @Autowired
    private CourseService courseService;
    
    @Autowired
    private ChapterMapper chapterMapper;
    
    @Autowired
    private LessonMapper lessonMapper;
    
    /**
     * 获取课程详情 - 优化版本，减少加载时间
     * 仅加载基本信息，章节信息按需加载
     */
    @GetMapping("/{id}/simple")
    public CommonResponse<CourseDetailDTO> getCourseSimpleDetail(
            @PathVariable Integer id,
            @RequestParam(required = false) Integer studentId
    ) {
        log.info("获取课程简要详情 - 课程ID: {}, 学生ID: {}", id, studentId);
        try {
            CourseDetailDTO course = courseService.getCourseBasicDetail(id, studentId);
            if (course == null) {
                log.warn("课程不存在 - 课程ID: {}", id);
                return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "课程不存在");
            }
            
            // 检查课程是否有章节数据
            if (course.getChapters() == null || course.getChapters().isEmpty()) {
                log.info("课程没有章节数据，尝试从数据库获取 - 课程ID: {}", id);
                // 如果章节列表为空，尝试从数据库加载章节信息
                try {
                    // 检查数据库中是否有章节数据
                    List<Chapter> chapters = chapterMapper.selectByCourseId(id);
                    log.info("找到章节数量: {}", chapters != null ? chapters.size() : 0);
                } catch (Exception e) {
                    log.error("获取章节信息失败: {}", e.getMessage(), e);
                }
            }
            
            log.info("课程详情获取成功 - 课程ID: {}, 课程标题: {}", id, course.getTitle());
            return CommonResponse.createForSuccess(ResponseCode.SUCCESS.getCode(), "查询成功", course);
        } catch (Exception e) {
            log.error("获取课程详情失败 - 课程ID: {}, 错误: {}", id, e.getMessage(), e);
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "获取课程详情失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取课程章节列表
     */
    @GetMapping("/{courseId}/chapters")
    public CommonResponse<?> getChapters(@PathVariable Integer courseId) {
        log.info("获取课程章节列表 - 课程ID: {}", courseId);
        try {
            List<Chapter> chapters = chapterMapper.selectByCourseId(courseId);
            return CommonResponse.createForSuccess(ResponseCode.SUCCESS.getCode(), "获取章节成功", chapters);
        } catch (Exception e) {
            log.error("获取章节列表失败 - 课程ID: {}, 错误: {}", courseId, e.getMessage(), e);
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "获取章节失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取章节下的课时列表
     */
    @GetMapping("/chapters/{chapterId}/lessons")
    public CommonResponse<?> getLessons(@PathVariable Integer chapterId) {
        log.info("获取章节课时列表 - 章节ID: {}", chapterId);
        try {
            List<Lesson> lessons = lessonMapper.selectByChapterId(chapterId);
            return CommonResponse.createForSuccess(ResponseCode.SUCCESS.getCode(), "获取课时成功", lessons);
        } catch (Exception e) {
            log.error("获取课时列表失败 - 章节ID: {}, 错误: {}", chapterId, e.getMessage(), e);
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "获取课时失败: " + e.getMessage());
        }
    }
} 